"""
AudioProcessor 入口类
- 保持原有 API（类名与方法签名）兼容
- 内部委托到子模块：基础I/O、编辑、合并、预览、视频提取
"""

from pydub import AudioSegment
from typing import List, Optional, Union

from .base_audio import load_audio, save_audio
from .edit_ops import (
    reverse_audio as _reverse,
    cut_audio as _cut,
    remove_segment as _remove,
    adjust_volume as _vol,
    change_speed as _speed,
    fade_in as _fade_in,
    fade_out as _fade_out,
    add_silence as _add_silence,
)
from .merge_ops import merge_audios as _merge, merge_audios_with_gaps as _merge_gaps
from .preview_ops import preview_audio as _preview_audio, preview_operation as _preview_op
from .video_ops import (
    get_video_audio_info as _video_info,
    extract_audio_from_video as _extract_from_video,
    extract_audio_with_pydub as _extract_with_pydub,
)


class AudioProcessor:
    """
    音频处理入口类
    - 将调用分发至各子模块，实现职责分离与低耦合
    """

    @staticmethod
    def load_audio(file_path: str) -> AudioSegment:
        """
        加载音频文件
        """
        return load_audio(file_path)

    @staticmethod
    def save_audio(audio: AudioSegment, output_path: str) -> None:
        """
        保存音频到指定路径
        """
        save_audio(audio, output_path)

    @staticmethod
    def reverse_audio(input_path: str, output_path: str) -> None:
        """
        倒放音频
        """
        _reverse(input_path, output_path)

    @staticmethod
    def cut_audio(input_path: str, output_path: str, start_ms: int, end_ms: int) -> None:
        """
        剪切区间 [start_ms, end_ms)
        """
        _cut(input_path, output_path, start_ms, end_ms)

    @staticmethod
    def remove_segment(input_path: str, output_path: str, start_ms: int, end_ms: int) -> None:
        """
        删除区间 [start_ms, end_ms)，并拼接首尾
        """
        _remove(input_path, output_path, start_ms, end_ms)

    @staticmethod
    def merge_audios(input_paths: List[str], output_path: str) -> None:
        """
        合并多个音频
        """
        _merge(input_paths, output_path)

    @staticmethod
    def adjust_volume(input_path: str, output_path: str, volume_db: float) -> None:
        """
        调整音量（分贝）
        """
        _vol(input_path, output_path, volume_db)

    @staticmethod
    def change_speed(input_path: str, output_path: str, speed_factor: float) -> None:
        """
        改变播放速度（不改变音调）
        """
        _speed(input_path, output_path, speed_factor)

    @staticmethod
    def fade_in(input_path: str, output_path: str, fade_ms: int) -> None:
        """
        添加淡入
        """
        _fade_in(input_path, output_path, fade_ms)

    @staticmethod
    def fade_out(input_path: str, output_path: str, fade_ms: int) -> None:
        """
        添加淡出
        """
        _fade_out(input_path, output_path, fade_ms)

    @staticmethod
    def preview_audio(audio_data_or_path: Union[str, AudioSegment], start_ms: int = 0, duration_ms: Optional[int] = None) -> None:
        """
        预览音频或片段
        """
        _preview_audio(audio_data_or_path, start_ms, duration_ms)

    @staticmethod
    def preview_operation(input_paths, operation_func, *args, **kwargs) -> None:
        """
        预览指定的编辑/合并操作的结果（不落盘）
        """
        _preview_op(input_paths, operation_func, *args, **kwargs)

    @staticmethod
    def merge_audios_with_gaps(input_paths: List[str], output_path: str, gaps_ms: Optional[list] = None) -> None:
        """
        合并并插入静音间隙
        """
        _merge_gaps(input_paths, output_path, gaps_ms)

    @staticmethod
    def add_silence(input_path: str, output_path: str, position_ms: int, duration_ms: int) -> None:
        """
        插入静音
        """
        _add_silence(input_path, output_path, position_ms, duration_ms)

    @staticmethod
    def get_video_audio_info(video_path: str) -> dict:
        """
        获取视频音频流信息
        """
        return _video_info(video_path)

    @staticmethod
    def extract_audio_from_video(video_path: str, output_path: str, audio_format: str = "mp3", audio_bitrate: str = "192k") -> bool:
        """
        使用 ffmpeg 从视频中提取音频
        """
        return _extract_from_video(video_path, output_path, audio_format, audio_bitrate)

    @staticmethod
    def extract_audio_with_pydub(video_path: str, output_path: str) -> bool:
        """
        使用 pydub 备选方案提取音频
        """
        return _extract_with_pydub(video_path, output_path)